//
//  PayAmountVC.swift
//  RPay
//
//  Created by CharlieLiao on 2017/7/30.
//  Copyright © 2017年 Charlie. All rights reserved.
//

/// 支付金额
class PayAmountVC: AmountVC, CTAPIManagerParamSource, CTAPIManagerCallBackDelegate {
    
    // MARK: Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocalStr("支付金额")
        detailLabel.text =  LocalStr("支付金额")
        // Do any additional setup after loading the view.
        if Double(money)! > 0  {
            amountTF.isUserInteractionEnabled = false
            amountTF.text = money
        } else {
            amountTF.becomeFirstResponder()
        }
    }
    
    // MARK: CTAPIManagerParamSource
    func params(forApi manager: CTAPIBaseManager!) -> [AnyHashable : Any]!
    {
        return [kApiParamPayUserName: UserShare.shared.loginUser!.userName,
                kApiParamReceiveUserName: userName,
                kApiParamPerAmount: amountTF.text!,
                kApiParamTransferType: kApiParamTransferTypeReceive,
                kApiParamGenerateTime: generaltime]
    }
    
    // MARK: CTAPIManagerCallBackDelegate
    func managerCallAPIDidSuccess(_ manager: CTAPIBaseManager!) {
        hideLoading()
        let vc = PaySucceedVC()
        vc.price = amountTF.text!
        UserShare.shared.loginUser!.rhAccount -= Double(amountTF.text!)!
        self.navigationController!.pushViewController(vc, animated: true)
        _transferApiManager = nil
    }
    
    func managerCallAPIDidFailed(_ manager: CTAPIBaseManager!) {
        hideLoading()
        alert(manager.errorMessage)
        _transferApiManager = nil
    }
    
    // MARK: Event respond
    ///
    override func tapSumbitBtn()
    {
        amountTF.resignFirstResponder()
        
        if UserShare.shared.loginUser!.userName == userName {
            alert(LocalStr("不能给自己转账"))
            return
        }
        
        let money = Double(amountTF.text!)
        if money == nil || money! <= 0 {
            alert(LocalStr("请输入有效的金额"))
            return
        }
        
        let vc = ConfirmPayVC()
        vc.money = amountTF.text!
        vc.didConfirmBtn = { [weak self] (vc) in
            self?.showLoading()
            self?.transferApiManager.loadData()
        }
        present(vc, animated: true, completion: nil)
    }
    
    ///
    override func editEnd() {
        if let money = Double(amountTF.text!) {
            amountTF.text = String(format: "%.2f", money)
        } else {
            amountTF.text = ""
        }
    }
    
    // MARK:  Get
    var money: String!
    var userName: String!
    var generaltime: String!
    
    var _transferApiManager: TransferApiManager?
    var transferApiManager: TransferApiManager {
        if _transferApiManager == nil {
            _transferApiManager = TransferApiManager()
            _transferApiManager!.paramSource = self
            _transferApiManager!.delegate = self
        }
        return _transferApiManager!
    }
}
